﻿<?php
error_reporting(E_ERROR|E_PARSE);
set_time_limit(0);
session_start();
if($_SESSION["islogin"]!=='igiveyouthepower'){
echo "<script language=JavaScript>\r\n";
echo "alert('请先登录!');\r\n";
echo "location.href='index.php'\r\n";
echo "</script>";
}else{
require_once('config.php');
?>
<?php
$conn = mysqli_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,$cfg_dbname);
// $path = './';
// $result = scanFile($path);
// print_r($result);
// function scanFile($path){
// global $result;
// if($handle = opendir($path)){
//   while (false !== ($file = readdir($handle))){
//   if(is_file($file)){
// //unlink("uploadimg/000n04hp1535455115.jpg");
// $result[] = "$file 存在<br />";
// }else{
// if(is_dir($file)&&($file!=="."&&$file!=="..")){
// //unlink("uploadimg/000n04hp1535455115.jpg");
// $result[] = "$file 目录<br />";
// }else{
// $result[] = "$file 不存在<br />";
// }
// }

//   }
//   closedir($handle);
// }
// return $result;
// }
//遍历目录输出目录及其下的所有文件 利用函数的递归解决
function read_dir_queue($dir){
	$files=array();
	$queue=array($dir);
	while($data=each($queue)){
		$path=$data['value'];
		if(is_dir($path) && $handle=opendir($path)){
			while($file=readdir($handle)){
				if($file=='.'||$file=='..') continue;
				$files[] = $real_path=$path.'/'.$file;
				if (is_dir($real_path)) $queue[] = $real_path;
			}
		}
		closedir($handle);
	}
	 return $files;
}
$picArr = read_dir_queue('files');//返回的是数组格式的数据


$sqlm="select * from ph_img where imgpath like'%/files/%' order by id asc";
$resultm=mysqli_query($conn,$sqlm);
while($rowm=mysqli_fetch_array($resultm)){
	$bb[]=substr($rowm["imgpath"],1);
}

$c = array_diff($picArr,$bb);
foreach ($c as $age) {
	if(is_file("$age")){
	   unlink("./$age");
       //print_r("$age<br />");
	}
  
  //print_r("$c[0]<br />");
}
// echo "<pre>";
// print_r($c);
// echo "</pre>";
echo "全部清理完毕";
?>
<?php }?>